#include "time1c1.h"

DPOOL_txDIF_tstClkDpoolData DPOOL_txDIF_stClkDpoolData = {0,0,0};
void DIF__vClockMain(void)
{
    /***************************************************************************
    * FUNCTION LOCAL DEFINITIONS
    *
    * In this section define
    * - all function local type definitions
    * - all function local variables (auto and DIF_nSTATIC)
    ****************************************************************************/
    /*
    * Description  : dpool structure declaration
    */
    SYN__tstClockDpoolData stClockData;

    /*
    * Description  : CAN signal variables
    */
    uint8 u8CanHour = 0;
    uint8 u8CanMinute = 0;
    uint8 u8CanTimeMode = 0;

    /******************************************************************************
    * FUNCTION CODE
    ******************************************************************************/
    
    /* Read current time data from Dpool */
    SYN__enGetDpoolDataTime(&stClockData);
    
    /* Check if CAN signal for clock was received */
    if (DIF__boCanClkHUT7SigRx_Stat == True)
    {
        /* Reset the CAN receive status */
        DIF__boCanClkHUT7SigRx_Stat = False;
        
        /* Read CAN signals for time information */
        /* Note: These functions need to be implemented according to your CAN interface */
        u8CanHour = Time_Hour_ReadFn();        /* Read hour from CAN */
        u8CanMinute = Time_Minutes_ReadFn();   /* Read minute from CAN */
        u8CanTimeMode = TimeMod_ReadFn();      /* Read time mode from CAN */
        
        /* Validate the received time values */
        if (u8CanHour < 24 && u8CanMinute < 60)
        {
            /* Update Dpool data with received CAN values */
            stClockData.DIF_u16Hour = u8CanHour;
            stClockData.DIF_u16Min = u8CanMinute;
            stClockData.DIF_u16Mode = u8CanTimeMode;
            
            /* Also update the hardware clock with the new time */
            CLOCK_tunTime stClkWrTime;
            CLOCK_tunStatus stClkWrStatus;
            
            stClkWrTime.stTime.u8Hour = u8CanHour;
            stClkWrTime.stTime.u8Minute = u8CanMinute;
            stClkWrTime.stTime.u8Second = 0; /* Seconds set to 0 by default */
            
            /* Write new time to clock hardware */
            CLOCK_u16InterfaceWriteTime(&stClkWrTime, &stClkWrStatus);
        }
    }
    else
    {
        /* No CAN signal received, read current time from hardware clock */
        CLOCK_tunTime stClkRdTime;
        CLOCK_tunStatus stClkRdStatus;
        
        CLOCK_vInterfaceReadTime(&stClkRdTime, &stClkRdStatus);
        
        /* Update Dpool with current hardware time */
        stClockData.DIF_u16Hour = stClkRdTime.stTime.u8Hour;
        stClockData.DIF_u16Min = stClkRdTime.stTime.u8Minute;
        /* Mode remains unchanged if no CAN signal */
    }
    
    /* Update power off time calculation */
    DIF__vPowOffTimeCalcu();
    
    /* Set the updated Dpool data for application use */
    SYN__enSetDpoolDataTime(&stClockData);
}